package ga.selectionMethods;

import ga.GARun;
import ga.individuals.Individual;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class Elite implements SelectionReplacementMethod {
	private Integer numberOfElements;
	
	public Elite(Integer numberOfElements) throws IllegalArgumentException {
		if (numberOfElements < 0 || numberOfElements > GARun.popSize) {
			throw new IllegalArgumentException("Invalid number of elements!");
		}
		this.numberOfElements = numberOfElements;
	}
	
	@Override
	public Collection<Individual> method(Collection<Individual> individuals) {
		List<Individual> listIndividuals = new ArrayList<Individual>(individuals); 
		if (this.numberOfElements >= individuals.size()) {
			return listIndividuals;
		}
		Collections.sort(listIndividuals);
		Collections.reverse(listIndividuals);
		return listIndividuals.subList(0, this.numberOfElements);
	}

}
